﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.BackgroundAudio;
using AudioPlaybackAgent;
using System.Windows.Media.Imaging;
using MusicControl.ControlTik;

namespace DoubanFMApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        string url = @"http://douban.fm/j/mine/playlist?from=ie9&sid=641537&channel=1&type=s";
        public MainPage()
        {
            InitializeComponent();

            if (BackgroundAudioPlayer.Instance.Track == null)
            {
                BackgroundAudioPlayer.Instance.Volume = 1.0;
                BackgroundAudioPlayer.Instance.InitPlayList(url);
            }
            if (backgroundAudioBinding1.AudioTrack == null)
            {
                backgroundAudioBinding1.AudioTrack = BackgroundAudioPlayer.Instance.Track;
            }
            if (backgroundAudioBinding1.Position == null)
            {
                backgroundAudioBinding1.Position = BackgroundAudioPlayer.Instance.Position;
            }
            if (backgroundAudioBinding1.Duration == null)
            {
                backgroundAudioBinding1.Duration = BackgroundAudioPlayer.Instance.Track.Duration;
            }
            backgroundAudioBinding1.CurrentState = BackgroundAudioPlayer.Instance.PlayerState;
            BackgroundAudioPlayer.Instance.PlayStateChanged += new EventHandler(Instance_PlayStateChanged);
        }

        void Instance_PlayStateChanged(object sender, EventArgs e)
        {
            switch (BackgroundAudioPlayer.Instance.PlayerState)
            {
                case PlayState.Playing:
                    VisualStateManager.GoToState(this, "FadeOutStatus", true); 
                    backgroundAudioBinding1.AudioTrack = BackgroundAudioPlayer.Instance.Track;
                    VisualStateManager.GoToState(this, "FadeInStatus", true); 
                    break;
                default:
                    BackgroundAudioPlayer.Instance.Play(); 
                    break;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            BackgroundAudioPlayer.Instance.InitPlayList(url);
        }


    }
}